package com.fy.tt.util;

import java.io.*;

public class GenerateJar {
    final static String languagePath = "G:\\test3\\languageExecutor";


    public static void generateJar() {
        String command1 = "dir / s / B *.java > sources.txt";
        String command2 = "javac -d classes -cp .;.\\lib\\* @sources.txt -encoding utf-8 -Xlint:unchecked";
        String command3 = "jar cvfm ..\\Executor.jar .\\META-INF\\MANIFEST.MF -C .\\classes .";
        CmdExec.cmd(command1+"&&"+command2+"&&"+command3,languagePath);
    }


    public static  void replaceLanguagePackagePath(String path,String bandId) {
        File file = new File(path);
        if (file.exists()) {
            File[] files = file.listFiles();
            if (null == files || files.length == 0) {
                System.out.println("文件夹是空的!");
                return;
            } else {
                for (File file2 : files) {
                    if (file2.isDirectory()) {
                        replaceLanguagePackagePath(file2.getAbsolutePath(),bandId);
                    } else {
//                        if (file2.getName().contains(".java")&&!file2.getName().contains("My")) {
                        if (file2.getName().contains(".java")||file2.getName().contains(".cla")) {
                            alterStringToCreateNewFile("node.word;","node.word"+bandId+";",file2.getPath());
                            alterStringToCreateNewFile("node.word.","node.word"+bandId+".",file2.getPath());
                        }

                        System.out.println("文件:" + file2.getAbsolutePath());
                    }
                }
            }
        } else {
            System.out.println("文件不存在!");
        }
    }


    //        private static String path ="src/源文件"; //目标文件路径
//        private static File file = new File(path); //创建目标文件
    public static void alterStringToCreateNewFile( String oldString,
                                                   String newString ,String filePath){

        File file = new File(filePath);
        try {
            long start = System.currentTimeMillis(); //开始时间
            BufferedReader br = new BufferedReader(
                    new InputStreamReader(
                            new FileInputStream( file))); //创建对目标文件读取流
            File newFile = new File("src/newFile"); //创建临时文件
            if (!newFile.exists()){
                newFile.createNewFile(); //不存在则创建
            }
            //创建对临时文件输出流，并追加
            BufferedWriter bw = new BufferedWriter(
                    new OutputStreamWriter(
                            new FileOutputStream(newFile,true)));
            String string = null; //存储对目标文件读取的内容
            int sum = 0; //替换次数
            while ((string = br.readLine()) != null){
                //判断读取的内容是否包含原字符串
                if (string.contains(oldString)){
                    //替换读取内容中的原字符串为新字符串
                    string = new String(
                            string.replace(oldString,newString));
                    sum++;
                }
                bw.write(string);
                bw.newLine(); //添加换行
            }
            br.close(); //关闭流，对文件进行删除等操作需先关闭文件流操作
            bw.close();
            file.delete(); //删除源文件
            newFile.renameTo(new File(filePath)); //将新文件更名为源文件
            long time = System.currentTimeMillis() - start; //整个操作所用时间;
            System.out.println(sum+"个"+oldString+"替换成"+newString+"耗费时间:"+time);
        } catch(Exception e){
            System.out.println(e.getMessage());
        }
    }

}
